<template>
  <!-- 物料组表单弹窗 -->
  <el-dialog
    :close-on-click-modal="false"
    v-model="visibleDialog"
    :title="props.title"
    width="375px"
    draggable
    @close="closeDialog"
  >
    <el-form ref="formRef" :model="formData" :rules="rules" label-width="100px">
      <el-form-item label="工厂" prop="plantCode">
        <el-select
          v-if="!props.isReadonly"
          v-model="formData.plantCode"
          placeholder="请选择工厂"
          filterable
          clearable
          style="width: 200px"
        >
          <el-option
            v-for="item in plantList"
            :key="item.id"
            :label="item.organizationCode"
            :value="item.organizationCode"
          />
        </el-select>

        <span v-else>{{ formData.plantCode }}</span>
      </el-form-item>
      <el-form-item label="物料组编号" prop="materialGroupCode">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.materialGroupCode"
          placeholder="请输入物料组编号"
          clearable
          style="width: 200px"
        />

        <span class="word" v-else>{{ formData.materialGroupCode }}</span>
      </el-form-item>
      <el-form-item label="物料组名称" prop="materialGroupName">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.materialGroupName"
          placeholder="请输入物料组名称"
          clearable
          style="width: 200px"
        />

        <span class="word" v-else>{{ formData.materialGroupName }}</span>
      </el-form-item>
      <el-form-item label="启用状态" prop="enabled">
        <el-radio-group v-model="formData.enabled" v-if="!props.isReadonly">
          <el-radio :label="1">启用</el-radio>
          <el-radio :label="0">停用</el-radio>
        </el-radio-group>

        <span v-else>{{ formData.enabled == 1 ? "启用" : "停用" }}</span>
      </el-form-item>
      <el-form-item label="备注" prop="remarks">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.remarks"
          placeholder="请输入备注"
          clearable
          style="width: 200px"
        />

        <span class="word" v-else>{{ formData.remarks }}</span>
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime" v-if="props.isReadonly">
        <span>{{ formData.createTime }}</span>
      </el-form-item>
      <el-form-item
        label="创建人"
        prop="createdUserName"
        v-if="props.isReadonly"
      >
        <span>{{ formData.createdUserName }}</span>
      </el-form-item>
      <el-form-item
        label="修改时间"
        prop="lastUpdateTime"
        v-if="props.isReadonly"
      >
        <span>{{ formData.lastUpdateTime }}</span>
      </el-form-item>
      <el-form-item
        label="修改人"
        prop="lastUpdatedUserName"
        v-if="props.isReadonly"
      >
        <span>{{ formData.lastUpdatedUserName }}</span>
      </el-form-item>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button
          v-if="!props.isReadonly"
          type="primary"
          :loading="loading"
          @click="handleSubmit"
          >确 定</el-button
        >
        <el-button @click="closeDialog">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import request from "@/utils/request";
import { useDictStoreHook } from "@/store/modules/dictionary"; // 引入
const dictionaryStore = useDictStoreHook();

defineOptions({
  name: "MDMMDMMATGROUPADDORUPDATE",
  inheritAttrs: false,
});

const emit = defineEmits(["reset-table", "change-visible"]);

const props = defineProps({
  visible: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
  title: {
    type: String,
    default: () => {
      return "";
    },
  },
  data: {
    type: Object,
    default: () => {
      return {};
    },
  },
  isReadonly: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
});

const visibleDialog = ref(false);
watch(
  () => props.visible,
  (newVal: boolean) => {
    visibleDialog.value = newVal;
  }
);

const rules = reactive({
  materialGroupCode: [
    {
      required: true,
      pattern: "[^ \x20]+",
      message: "请输入物料组编号",
      trigger: "blur",
    },
    {
      required: true,
      message: "请输入物料组编号",
      trigger: "blur",
    },
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  materialGroupName: [
    {
      required: true,
      pattern: "[^ \x20]+",
      message: "请输入物料组名称",
      trigger: "blur",
    },
    {
      required: true,
      message: "请输入物料组名称",
      trigger: "blur",
    },
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  enabled: [],
  remarks: [
    { min: 1, max: 255, message: "长度在 1 到 255 个字符", trigger: "blur" },
  ],
  createTime: [],
  createdUserName: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  lastUpdateTime: [],
  lastUpdatedUserName: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  plantCode: [{ required: true, message: "请输入工厂", trigger: "change" }],
});

const formRef = ref(ElForm);

let formData = reactive({
  id: "",
  version: "",
  materialGroupCode: "",
  materialGroupName: "",
  enabled: 1,
  remarks: "",
  deleted: "",
  createTime: "",
  createdUserId: "",
  createdUserName: "",
  lastUpdateTime: "",
  lastUpdatedUserId: "",
  lastUpdatedUserName: "",
  plantCode: "",
  plantId: "",
});

watch(
  () => props.data,
  (newVal: any) => {
    formData.id = "";
    if (newVal?.id) {
      const {
        id,
        version,
        materialGroupCode,
        materialGroupName,
        enabled,
        remarks,
        deleted,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
        plantCode,
        plantId,
      } = newVal;
      Object.assign(formData, {
        id,
        version,
        materialGroupCode,
        materialGroupName,
        enabled,
        remarks,
        deleted,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
        plantCode,
        plantId,
      });
    }
    // else {
    //   formData.enabled = 1;
    // }
  }
);

/** 关闭表单弹窗 */
function closeDialog() {
  visibleDialog.value = false;
  emit("change-visible", false);
  resetForm();
}

/** 重置表单 */
function resetForm() {
  formRef.value.resetFields();
  formData.id = "";
  formData.version = "";
  formData.materialGroupCode = "";
  formData.materialGroupName = "";
  formData.enabled = 1;
  formData.remarks = "";
  formData.deleted = "";
  formData.createTime = "";
  formData.createdUserId = "";
  formData.createdUserName = "";
  formData.lastUpdateTime = "";
  formData.lastUpdatedUserId = "";
  formData.lastUpdatedUserName = "";
  formData.plantCode = "";
  formData.plantId = "";
}

const loading = ref(false);

/** 物料组保存提交 */
function handleSubmit() {
  formRef.value.validate(async (valid: any) => {
    if (valid) {
      for (let i = 0; i < plantList.value.length; i++) {
        if (plantList.value[i].organizationCode == formData.plantCode) {
          formData.plantId = plantList.value[i].id;
        }
      }
      loading.value = true;
      const text = formData.id ? "编辑" : "新增";
      await request({
        url: "/mdm/mdmmatgroup/save",
        method: "post",
        data: formData,
      }).finally(() => {
        loading.value = false;
      });
      ElMessage.success(text + `成功`);
      closeDialog();
      emit("reset-table", formData.id);
    }
  });
}

const plantList: any = ref([]);

const getPlantList = async () => {
  const res1 = await request({
    url: "system/sysOrganization/getList",
    method: "post",
    data: { organizationType: 3 },
  });
  plantList.value = res1.data;
};

onMounted(() => {
  getPlantList();
});
// 组件销毁时调用
onBeforeUnmount(() => {
  visibleDialog.value = false;
  emit("change-visible", false);
});
</script>
<style lang="scss" scoped>
.word {
  word-wrap: break-word;
  width: 300px;
  /* 设置容器宽度 */
}
</style>
